package FIT::Util::HTMLBuilder;

use strict;
use warnings;
use Carp;

my @TAGS = qw(HTML BODY P A BR FORM TABLE TBODY TR TD);

my %NO_CLOSE = map { $_ => 1 } qw(BR);

sub import {
    no strict 'refs';
    my $class   = shift;
    my $package = caller();
    for my $tag (@TAGS) {
        *{"${package}::$tag"} = sub {
            local $_;
            my @params = @_;
            my $lc_tag = lc $tag;

            my @attrs    = grep { ref eq 'HASH' } @params;
            my @children = grep { !ref          } @params;

            return
                "<$lc_tag".
                    (
                        @attrs?
                            q[ ]. join(
                                q[ ],
                                map { attrs_as_string($_) } @attrs
                            ): q[]
                    )
                . ">".
                join(q[], @children).
                ($NO_CLOSE{$tag}? q[]: "</$lc_tag>")
        };
    }
}

sub attrs_as_string {
    my $attr = shift;
    local $_;
    return join '', map { qq{$_="$attr->{$_}"} } keys %$attr;
}

1;

